package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.DoctorMapper;
import com.kkb.pojo.Doctor;
import com.kkb.pojo.DoctorExample;
import com.kkb.vo.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author:CoolTiger
 * @title: DoctorService
 * @projectName ssm_hospital
 * @description: TODO 医生业务层
 * @date:2022/07/09/14:55:06
 **/
@Service
public class DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * TODO 医生多条件查询业务
     * @param pagaNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Doctor> queryAll(Integer pagaNum, Integer pageSize, DoctorVo vo){
        //分页
        DoctorExample example = new DoctorExample();
        //创建分页条件容器
        DoctorExample.Criteria criteria = example.createCriteria();
        if (vo != null){
            if (vo.getDoctorId() != null){
                criteria.andDIdEqualTo(vo.getDoctorId());
            }
            if (vo.getDoctorName() != null && !"".equals(vo.getDoctorName().trim())){
                criteria.andDNameLike("%"+vo.getDoctorName()+"%");
            }
            if (vo.getDoctorKeshi() != null && !"".equals(vo.getDoctorKeshi().trim())){
                criteria.andDKeshiLike("%"+vo.getDoctorKeshi()+"%");
            }
        }
        //多条件
        PageHelper.startPage(pagaNum,pageSize);
        List<Doctor> doctors = doctorMapper.selectByExample(example);
        return new PageInfo<>(doctors);
    }
    /**
     * TODO 医生信息添加业务
     * @param doctor
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addDoctor(Doctor doctor){
        return doctorMapper.insertSelective(doctor);
    }

    /**
     * TODO 医生离职删除业务
     * @param doctorId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteDoctor(Integer doctorId){
        Doctor doctor = doctorMapper.selectByPrimaryKey(doctorId);
        doctor.setdState(1);
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }
    /**
     * TODO 医生信息回显
     * @param doctorId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Doctor queryById(Integer doctorId){
        Doctor doctor = doctorMapper.selectByPrimaryKey(doctorId);
        return doctor;
    }
    /**
     * TODO 医生信息更新
     * @param doctor
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateDoctor(Doctor doctor){
        return doctorMapper.updateByPrimaryKeySelective(doctor);
    }


}
